package org.example.struct.array;

import org.example.node.ListNode;

import java.util.HashSet;
import java.util.Set;

/**
 * @author liyishan
 * @date 2024/9/5 10:41
 * @apiNote
 */

public class M0201RemoveDuplicateNodes {
    public ListNode removeDuplicateNodes(ListNode head) {
        Set<Integer> set = new HashSet<>();
        ListNode cur = head;
        ListNode dummpy = new ListNode(0);
        ListNode res = dummpy;
        while(cur != null){
            if(!set.contains(cur.val)){
                ListNode temp = new ListNode(cur.val);
                dummpy.next = temp;
                dummpy = dummpy.next;
                set.add(cur.val);
            }
            cur = cur.next;
        }
        return res.next;
    }
    public ListNode removeDuplicateNodes1(ListNode head) {
        ListNode ob = head;
        while(ob != null){
            ListNode oc = ob;
            while(oc.next != null){
                if(oc.next.val == ob.val){
                    oc.next = oc.next.next;
                }else{
                    oc = oc.next;
                }
            }
            ob = ob.next;
        }
        return head;

    }

}
